package springCloud.controller;

import com.luoqiu.entities.Payment;
import com.luoqiu.entities.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springCloud.service.PaymentService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 洛秋y
 * @date 2020/11/9 13:18
 */
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping(value = "/get")
    public Result<Payment> get() {
        Payment payment = paymentService.get();
        return Result.success(payment);
    }

    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody Payment payment) {
        paymentService.add(payment);
        return Result.success("ok");
    }

    /**
     * 获取该微服务下所有的注册的服务信息
     * @return
     */
    @GetMapping(value = "/dis")
    public Object dis() {
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost());
        }
        return instances;
    }
}
